package org.darkforest.ucenter.service.impl;

import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.ComplainMapper;
import org.darkforest.dao.mapper.OrderMapper;
import org.darkforest.enums.ComplainStateEnum;
import org.darkforest.enums.ComplainSubjectEnum;
import org.darkforest.model.Complain;
import org.darkforest.model.ComplainSearchBean;
import org.darkforest.model.ComplainVo;
import org.darkforest.model.OrderVo;
import org.darkforest.ucenter.service.ComplainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 项目名称         :bwmall-ucenter
 * 文件名           :ComplaintServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月21日)
 */
@Service
public class ComplainServiceImpl implements ComplainService {

	@Autowired
	private ComplainMapper complainMapper;
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public void getComplainList(ComplainSearchBean page) {
		page.setResultList(complainMapper.selectByMember(page));
	}

	@Override
	public void selectByCondition(ComplainSearchBean page) {
		page.setResultList(complainMapper.selectByCondition(page));
	}

	@Override
	public Integer addComplain(ComplainVo vo) {
		Integer id=0;
		Map<String, Integer> param = new HashMap<String, Integer>();
		param.put("id", vo.getOrderId());
		param.put("buyerId", vo.getAccuserId());
		OrderVo o = orderMapper.selectById(param);
		if(o != null){
			vo.setAccusedId(o.getStoreId());
			vo.setAccusedName(o.getStoreName());
			ComplainSubjectEnum e = ComplainSubjectEnum.getByValue(vo.getComplainSubjectId());
			if(e != null){
				vo.setComplainSubjectContent(e.getText());
			}
			vo.setComplainDatetime(DateUtil.unixTimeStampInt().intValue());
			vo.setComplainState(ComplainStateEnum.NEW_COMPLAIN.getValue());
			vo.setComplainActive(1);
			vo.setComplainHandleDatetime(0);
			vo.setComplainHandleMemberId(0);
			vo.setAppealMessage("");
			vo.setAppealDatetime(0);
			vo.setAppealPic1("");
			vo.setAppealPic2("");
			vo.setAppealPic3("");
			vo.setFinalHandleMessage("");
			vo.setFinalHandleDatetime(0);
			vo.setFinalHandleMemberId(0);
			id =complainMapper.insertSelective(vo);
		}
		return  id;
	}


	@Override
	public ComplainVo selectByPrimaryKey(Integer id) {
		return complainMapper.selectByPrimaryKey(id);
	}

	@Override
	public ComplainVo getComplainDetail(Integer orderId, Integer goodsId,
			Integer memberId) {
		Map<String, Object> p = new HashMap<String, Object>();
		p.put("orderId", orderId);
		p.put("goodId", goodsId);
		p.put("memberId", memberId);
		return complainMapper.selectByOrderGood(p);
		
	}

	@Override
	public void deleteByIds(List nList) {
		complainMapper.deleteByIds(nList);
	}

	@Override
	public void updateComplain(ComplainVo vo) {
		complainMapper.updateByPrimaryKeySelective(vo);
	}


	@Override
	public List<ComplainVo> selectComplainForMerchant(ComplainSearchBean page){
		return complainMapper.selectComplainForMerchant(page);
	}

}
